package logic

import (
	"context"
	"user_src/models"

	"user_src/internal/svc"
	"user_src/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *user.LoginRequest) (*user.LoginResponse, error) {
	// todo: add your logic here and delete this line

	//短信验证码校验
	//查询手机号码是否已注册
	var u models.User
	u.Mobile = in.Mobile
	err := u.GetUserInfoByMobile()
	if err != nil {
		logx.Error("登录失败", err)
		return nil, err
	}

	//如果未注册，新注册
	if u.ID == 0 {
		if err = u.Create(); err != nil {
			logx.Error("注册失败", err)
			return nil, err
		}
	}

	return &user.LoginResponse{
		UserId: int64(u.ID),
	}, nil
}
